
#include <stdio.h>
#include <stdbool.h>

#define SIZE 15

char board[SIZE][SIZE]; // 棋盘
int currentPlayer; // 当前玩家（1或2）

// 初始化棋盘
void initializeBoard() {
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            board[i][j] = ' ';
        }
    }
}

// 打印棋盘
void printBoard() {
    printf(" ");
    for (int i = 0; i < SIZE; i++) {
        printf(" %d", i);
    }
    printf("\n");
  
    for (int i = 0; i < SIZE; i++) {
        printf("%d ", i);
        for (int j = 0; j < SIZE; j++) {
            printf("%c|", board[i][j]);
        }
        printf("\n");
      
        if (i != SIZE - 1) {
            printf(" ");
            for (int j = 0; j < SIZE; j++) {
                printf("-+");
            }
            printf("\n");
        }
    }
}

// 判断是否胜利
bool isWin(int x, int y) {
    char target = board[x][y];
  
    // 水平方向
    int count = 1; // 记录相同棋子数
    int i = x - 1;
    while (i >= 0 && board[i][y] == target) {
        count++;
        i--;
    }
    i = x + 1;
    while (i < SIZE && board[i][y] == target) {
        count++;
        i++;
    }
    if (count >= 5) {
        return true;
    }
  
    // 竖直方向
    count = 1;
    int j = y - 1;
    while (j >= 0 && board[x][j] == target) {
        count++;
        j--;
    }
    j = y + 1;
    while (j < SIZE && board[x][j] == target) {
        count++;
        j++;
    }
    if (count >= 5) {
        return true;
    }
  
    // 左上到右下斜线方向
    count = 1;
    i = x - 1;
    j = y - 1;
    while (i >= 0 && j >= 0 && board[i][j] == target) {
        count++;
        i--;
        j--;
    }
    i = x + 1;
    j = y + 1;
    while (i < SIZE && j < SIZE && board[i][j] == target) {
        count++;
        i++;
        j++;
    }
    if (count >= 5) {
        return true;
    }
  
    // 右上到左下斜线方向
    count = 1;
    i = x - 1;
    j = y + 1;
    while (i >= 0 && j < SIZE && board[i][j] == target) {
        count++;
        i--;
        j++;
    }
    i = x + 1;
    j = y - 1;
    while (i < SIZE && j >= 0 && board[i][j] == target) {
        count++;
        i++;
        j--;
    }
    if (count >= 5) {
        return true;
    }
  
    return false;
}

// 下棋
bool makeMove(int x, int y) {
    if (board[x][y] != ' ') {
        return false; // 该位置已有棋子
    }
  
    char chess = (currentPlayer == 1) ? 'O' : 'X';
    board[x][y] = chess;
  
    return true;
}

int main() {
    initializeBoard();
    currentPlayer = 1;
  
    while (true) {
        printBoard();
      
        // 提示玩家下棋
        printf("轮到玩家 %d 下棋，请输入坐标（x y）：", currentPlayer);
      
        int x, y;
        scanf("%d %d", &x, &y);
      
        // 检查坐标合法性
        if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
            printf("无效的坐标，请重新输入！\n");
            continue;
        }
      
        // 下棋
        if (!makeMove(x, y)) {
            printf("该位置已有棋子，请重新输入！\n");
            continue;
        }
      
        // 判断胜负
        if (isWin(x, y)) {
            printBoard();
            printf("玩家 %d 获胜！\n", currentPlayer);
            break;
        }
      
        // 切换玩家
        currentPlayer = (currentPlayer == 1) ? 2 : 1;
    }
  
    return 0;
}